<?php
class Login{
  public $conn=null;

  /**
    * 链接数据库
    * @param $db 要链接的数据库
    */
 	public function connect($db){
 		// $this->conf=Conf::getIns();
 		$conn=mysqli_connect('127.0.0.1','root','root');
 		$res=mysqli_query($conn,"use $db");
     mysqli_query($conn,'set names utf8');
     $this->conn=$conn;
 	}
  /**
   * 执行sql查询
   * @param $sql 要执行的sql语句
   */
	public function query($sql){
		$rs=mysqli_query($this->conn,$sql);
		return $rs;
	}

	/**
   * 查询所有需要的数据
   * @param $sql 要执行的sql文件
   */
	public function getAll($sql){
		$rs=$this->query($sql);
		$data=array();
		while($row=mysqli_fetch_assoc($rs)){
			$data[]=$row;
		}
		return $data;

	}
  //增,改自动拼接语句
	public function autoExecute($table,$data,$act='insert',$where=1){
		if(empty($data)){
			return false;
		}
		if($act=='insert'){
			//insert into table (a,b,c) values ('a','b','c');
			$sql="insert into $table (" . implode(',',array_keys($data)) . ") values ('";
			$sql .=implode("','",array_values($data)) . "')";
			return $this->query($sql);
		}else if($act=='update'){
			//update table set a='a',b='b' where id=1;
			$sql="update $table set ";
			foreach($data as $k=>$v){
				$sql .=$k . "='" . $v . "',";
			}
			$sql=rtrim($sql,',') . ' where ' .$where;
      // echo $sql;
      // exit;
			return $this->query($sql);
		}
	}
?>
